package com.example.dayu521.msg_sender.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dayu521.msg_sender.service.ISeckillSessionService;
import com.example.dayu521.msg_sender.service.fengin.MsgReceiveFC;
import com.example.dayu521.msg_sender.service.fengin.MyselfFC;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 秒杀活动场次 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-20
 */
@Slf4j
@Controller
@RequestMapping("/seckillSession")
@RefreshScope
public class SeckillSessionController {

    @Value("${dd.name:''}")
    private String myself;

    @Resource
    private ISeckillSessionService iSeckillSessionService;

    @Resource
    private MsgReceiveFC msgReceiveFC;

    @Resource
    private MyselfFC myselfFC;
//    @Scheduled(cron="*/5 * * * * *")
    public void send_msg(){
        try {
            iSeckillSessionService.sync_send_msg2();
        }catch (DuplicateKeyException e){
            log.info("检测到其他client正在派发事件,当前client安全退出");
        }catch (Exception e){
            log.info("");
            throw e;
        }
    }

    @GetMapping("/send_msg")
    String send_msg2(){
        iSeckillSessionService.sync_send_msg2();
        return "hello";
    }

    public record Tx(Date date,String myself){

    }
    @GetMapping("/hello")
    @ResponseBody
    Tx hello(){
        return new Tx(new Date(),myself);
    }

    @GetMapping("/sku")
    @ResponseBody
    List<Map<String, Object>> sku(){
        return iSeckillSessionService.get_seckill_skus(new QueryWrapper<>().select("*"));
    }
    @GetMapping("/fegin/hello")
    @ResponseBody
    public String hexx(){
        return msgReceiveFC.hello();
    }

    @GetMapping("/fegin/myself")
    @ResponseBody
    public Tx myself(){
        return myselfFC.hello();
    }

}
